<template>
	<view class="login-container">
		<view class="uni-form-item">
			<text class="login_label">登录</text>
		</view>
		<view class="username">
			<text>用户名</text><input class="uni-input" v-model="username" />
		</view>
		<view class="password">
			<text>密码</text>
			<view class="pwd">
				<input class="uni-input" v-model="password" :password="!showPassword" style="width: 100%;" />
				<!-- 显示或隐藏密码 -->
				<image @click="()=>{showPassword=!showPassword;}"
					:src="showPassword?'/static/pwd/eye-open.png':'/static/pwd/eye-close.png'"
					style="position: absolute; z-index: 2;  height: 60rpx; width: 60rpx; margin-top: 20rpx; right: 20rpx;">
				</image>
			</view>

		</view>
		<view class="capt">
			<view class="capt-left">
				<text class="capt-text">验证码</text><input class="uni-input" v-model="capt" />
			</view>
			<image class="img" :src="imgSrc" @click="getCaptcha"></image>
		</view>
		<view class="btn">
			<view>
				<button type="primary" class="btn-login" @click="gotoRegister">去注册</button>
			</view>
			<view>
				<button type="primary" class="btn-login2" @click="loginUser">登录</button>
			</view>
			<view style="margin-top: 60rpx;">
				<text>登录即同意隐私政策</text>
			</view>
			<view style="margin-top: 50rpx;">
				<text>@Copyright By QAQ</text>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "login_page",
		data() {
			return {
				showPassword: false,
				timer: null,
				imgSrc: '',
				username: '',
				password: "",
				capt: '',
				captId: 0,
			};
		},
		onLoad() {
			this.timer = null
			this.getCaptcha()
		},
		computed: {
			...mapState('m_user', ['redirectInfo'])
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
			async getCaptcha() {
				if (this.timer) return uni.$showMsg("5秒后再点击")
				this.timer = setTimeout(() => {
					this.timer = null
				}, 5000)
				const res = await uni.$dapi.getCaptcha();
				console.log(res);
				if (res == null) return
				this.imgSrc = res.image
				this.captId = res.codeId
			},
			async loginUser() {
				if (this.check() == null) return;
				// const cc = await uni.$api.postLogin()
				// console.log(cc);
				const res = await uni.$dapi.postLogin({
					userName: this.username,
					password: this.password,
					code: this.capt,
					codeId: this.captId,
				});
				// console.log(res);
				if (res == null) return;
				this.updateToken(res.tokenValue)
				const user = await uni.$dapi.getUserInfo();
				// console.log(user);
				if (user == null) return;



				this.updateUserInfo(user)

				// 延迟返回
				setTimeout(() => {
					this.goBack();
					uni.$showMsg("登录成功")
					this.navigateBack()
				}, 2000)

			},
			goBack() {

				uni.navigateBack({
					delta: 1, //返回层数，2则上上页
				})
			},

			navigateBack() {
				if (this.redirectInfo && this.redirectInfo.openType === 'switchTab') {
					uni.switchTab({
						url: this.redirectInfo.from,
						complete: () => {
							this.updateRedirectInfo(null)
						}
					})
				}
			},
			gotoRegister() {
				uni.navigateTo({
					url: '../register_page/register_page'
				})
			},
			check() {
				if (this.username.length < 3 || this.username.length > 20) {
					return uni.$showMsg("用户名需3-20个字符");
				}
				if (this.password.length < 3 || this.password.length > 20) {
					return uni.$showMsg("密码需3-20个字符");
				}
				if (this.capt.length !== 5) {
					return uni.$showMsg("验证码格式不正确");
				}
				return true;
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		height: fit-content;
		padding: 20rpx 40rpx;
		// margin: 200rpx 20rpx;
		text-align: center;
		background-color: #c6d1d5;

		.username {
			margin-top: 30rpx;
		}

		.password {
			margin-top: 30rpx;

			.pwd {
				margin-top: 10rpx;
				display: flex;
				flex-direction: row;
				position: relative;
			}
		}

		.capt {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			margin-top: 30rpx;

			.capt-left {
				.capt-text {
					white-space: nowrap;
				}
			}

			.img {
				height: 100rpx;
				width: 300rpx;
				margin-top: 30rpx;
			}

		}

		.login_label {
			color: #ac1032;
			margin: 0 auto;
			font-size: 60rpx;
		}

		.uni-input {
			margin-top: 10rpx;
			background-color: #fff;
			height: 80rpx;
			text-align: left;
			padding-left: 10rpx;
			border-radius: 10rpx;
			font-size: 30rpx;
		}

		.uni-form-item {
			margin: 30rpx 0;
		}

		button {
			font-size: 30rpx;
		}

		.btn {
			// display: flex;
			// flex-direction: column;
			// justify-content: center;
			// align-items: center;
			margin-top: 200rpx;
			text-align: center;

			.btn-login {
				width: 90%;
				border-radius: 100px;
				margin: 15px auto;
				background-color: #00e5c3;
			}

			.btn-login2 {
				width: 90%;
				border-radius: 100px;
				margin: 15px auto;
				background-color: #C00000;
			}
		}

	}
</style>